package HomePage
{
	import Assets.Controls.AddEditContentWindow;
	import Assets.Controls.NewTab;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	import mvc.AbstractView;
	import mvc.Controller;
	
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.List;
	import mx.controls.TextArea;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	
	import util.Observable;

	public class HomePageView extends AbstractView
	{
		private var container_sprite:Sprite;
		private var nativeWindow_object:Object=new Object;
		private var homePageTab:NewTab;
		private var homePageModel:Object;
		private var homePageController:Object;
		
		private var children_array:Array;
		private var moduleNames_array:Array = new Array;
		private var modulePaths_array:Array = new Array;
		private var moduleDiscription_array:Array = new Array;
		
		private var lessonName_array:Array = new Array;
		private var lessonDiscription_array:Array = new Array;
		private var lessonPath_array:Array = new Array;
		private var lessonEditPass_array:Array = new Array;
		private var lessonInstructorPass_array:Array = new Array;
		
		private var module_list:List = new List;
		private var lesson_list:List = new List;
		//private var discriptionBox_textArea:TextArea = new TextArea;
		private var addModule_button:Button = new Button;
		private var editModule_button:Button = new Button;
		private var removeModule_button:Button = new Button;
		private var openModule_button:Button = new Button;
		private var importModule_button:Button = new Button;
		
		private var addLesson_button:Button = new Button;
		private var editLesson_button:Button = new Button;
		private var removeLesson_button:Button = new Button;
		private var openLesson_button:Button = new Button;
		private var openLessonInNewWindow_Button:Button = new Button;
		private var importLesson_button:Button = new Button;
		
		private var listColors_array:Array=new Array(0xdddddd,0xffffff);
				
		
		private var discriptionBox_panel:Panel = new Panel;
		private var discriptionBox_textArea:TextArea = new TextArea;
		
		//private var addEditLessonPanel:MyWindow;
			
		//private	var children_array:Array;
			
		
		
		
		[Embed(source="Assets/icons/homeTabImage.jpg")]
		[Bindable]
		private var homeTabBackground:Class;
		
		public function HomePageView(model:Observable, controller:Controller, container:Sprite, stage:Object)
		{
			super(model, controller);
			this.homePageController=this.getController();
			this.container_sprite=container;
			this.nativeWindow_object=stage;
			this.initHomePageTab();
			
			//this.container_sprite.addChild(this.homePageTab);
			/*var infoObj_object:Object = new Object;
			infoObj_object.message = "createNewModule";
			infoObj_object.moduleName = "TestModule";
			infoObj_object.moduleDiscription = "this is a test module";
			this.homePageModel=this.getModel();
			this.homePageModel.incomingMessageGateway(infoObj_object);*/
		}
		
		private function initHomePageTab():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.tabName = "My Spline";
			//infoObj_object.children = this.children_array;
			infoObj_object.tabBackground = this.homeTabBackground;
			
			this.homePageTab= new NewTab(this.container_sprite);
			this.homePageTab.setTabAppearance(infoObj_object);
			
			this.initModuleListControls();
			this.initLessonListControls();
			this.initDiscriptionPanel();
			this.addChildOnStage();
			
		}
		
		private function initModuleListControls():void
		{
			this.children_array = new Array;
			this.module_list.width=200;
			this.module_list.height= 300;
			this.module_list.x=40;
			this.module_list.y=20;
			this.module_list.setStyle("color","0x000000");
			this.module_list.setStyle("alternatingItemColors",this.listColors_array);
			this.module_list.editable=false;
			this.module_list.dataProvider=this.moduleNames_array;
			this.module_list.addEventListener(ListEvent.ITEM_CLICK,this.moduleListItemSelected);
			this.module_list.dataProvider = this.moduleNames_array;
			this.module_list.selectable=true;
			this.children_array.push(this.module_list);
			
			this.addModule_button.x=50;
			this.addModule_button.y=340;
			this.addModule_button.label="Add Module";
			this.addModule_button.addEventListener(MouseEvent.CLICK,this.addNewModule);
			this.children_array.push(this.addModule_button);
			
			this.editModule_button.x=50;
			this.editModule_button.y=390;
			this.editModule_button.label="Edit Module Info";
			this.editModule_button.addEventListener(MouseEvent.CLICK,this.editSelectedModule);
			this.children_array.push(this.editModule_button);
			
			this.removeModule_button.x=50;
			this.removeModule_button.y=440;
			this.removeModule_button.label="Remove Module";
			this.removeModule_button.addEventListener(MouseEvent.CLICK,this.removeSelectedModule);
			this.children_array.push(this.removeModule_button);
			
			this.openModule_button.x=50;
			this.openModule_button.y=490;
			this.openModule_button.label="Open Module";
			this.openModule_button.addEventListener(MouseEvent.CLICK,this.openSelectedModule);
			this.children_array.push(this.openModule_button);
			
			this.importModule_button.x=50;
			this.importModule_button.y=540;
			this.importModule_button.label = "Import Module";
			this.importModule_button.addEventListener(MouseEvent.CLICK,this.importNewModule);
			this.children_array.push(this.importModule_button);
		}
		
		private function initLessonListControls():void
		{
			this.lesson_list.width=200;
			this.lesson_list.height= 300;
			this.lesson_list.x=300;
			this.lesson_list.y=20;
			this.lesson_list.setStyle("color","0x000000");
			this.lesson_list.setStyle("alternatingItemColors",this.listColors_array);
			this.lesson_list.editable=false;
			this.lesson_list.dataProvider=this.lessonName_array;
			this.lesson_list.selectable=true;
			this.lesson_list.addEventListener(ListEvent.ITEM_CLICK,this.lessonListItemSelected);
			this.lesson_list.dataProvider = this.lessonName_array;
			this.children_array.push(this.lesson_list);
			
			this.addLesson_button.x=320;
			this.addLesson_button.y=340;
			this.addLesson_button.label="Add lesson";
			this.addLesson_button.addEventListener(MouseEvent.CLICK,this.addNewLesson);
			this.children_array.push(this.addLesson_button);
			
			this.editLesson_button.x=320;
			this.editLesson_button.y=390;
			this.editLesson_button.label="Edit Lesson Info";
			this.editLesson_button.addEventListener(MouseEvent.CLICK, this.editSelectedLesson);
			this.children_array.push(this.editLesson_button);
			
			this.removeLesson_button.x=320;
			this.removeLesson_button.y=440;
			this.removeLesson_button.label="Remove lesson";
			this.removeLesson_button.addEventListener(MouseEvent.CLICK, this.removeSelectedLesson);
			this.children_array.push(this.removeLesson_button);
			
			this.openLesson_button.x=320;
			this.openLesson_button.y=490;
			this.openLesson_button.label="Open in Tab";
			this.openLesson_button.addEventListener(MouseEvent.CLICK,this.openSelectedLessonInTab);
			this.children_array.push(this.openLesson_button);
			
			this.openLessonInNewWindow_Button.x=320;
			this.openLessonInNewWindow_Button.y=540;
			this.openLessonInNewWindow_Button.label="Open in Window";
			this.openLessonInNewWindow_Button.addEventListener(MouseEvent.CLICK,this.openSelectedLessonInWindow);
			this.children_array.push(this.openLessonInNewWindow_Button);
			
			this.importLesson_button.x = 320;
			this.importLesson_button.y=590;
			this.importLesson_button.label = "Import Lesson";
			this.importLesson_button.addEventListener(MouseEvent.CLICK, this.importNewLesson);
			this.children_array.push(this.importLesson_button);
		}
		
		private function initDiscriptionPanel():void
		{
			this.discriptionBox_panel.x= 600;
			this.discriptionBox_panel.y=50;
			this.discriptionBox_panel.width=200;
			this.discriptionBox_panel.height=200;
			
			this.discriptionBox_textArea.width=150;
			this.discriptionBox_textArea.height=150;
			this.discriptionBox_textArea.editable=false;
			this.discriptionBox_panel.addChild(this.discriptionBox_textArea);
			this.children_array.push(this.discriptionBox_panel);
		}
		
		private function addChildOnStage():void
		{
			for each(var displayObj:DisplayObject in this.children_array)
			{
				this.homePageTab.newTab.addChild(displayObj);
			}
		}
		
		private function moduleListItemSelected(event:ListEvent):void
		{
			this.discriptionBox_textArea.text=this.moduleDiscription_array[this.module_list.selectedIndex];
		}
		
		private function lessonListItemSelected(event:ListEvent):void
		{
			this.discriptionBox_textArea.text=this.lessonDiscription_array[this.lesson_list.selectedIndex];
		}
		
		//-----------------------------------------------------module related operations ----------------------------------------------------//
		private function addNewModule(event:MouseEvent):void
		{
			this.nativeWindow_object.enabled = false;
			var addEditContentWindow:AddEditContentWindow = new AddEditContentWindow("Module","AddModule",this.moduleNames_array.length,0,this);
			//sending the length as a parameter as this is an optional one u can send any other no. too :)
			//0 is for the selected lesson index which i this case is none			
		}
		
		private function editSelectedModule(event:MouseEvent):void
		{
			this.nativeWindow_object.enabled = false;
			var addEditContentWindow:AddEditContentWindow = new AddEditContentWindow("Module","EditModule",this.module_list.selectedIndex,0,this);
			
		}
		
		private function removeSelectedModule(event:MouseEvent):void
		{
			if(this.module_list.selectedIndex>=0)
			{
				Alert.show("Do you want to remove it from filesystem ?","Confirmation",Alert.YES|Alert.NO,null,this.completeModuleRemovalConfirmation,null,Alert.YES);
			}
			//this.sendInfoToController(infoObj_object);
			else
			{
				Alert.show("Select a module to be removed !!!!"); 
			}
		}
		
		private function completeModuleRemovalConfirmation(eventObj:CloseEvent):void
		{
			this.discriptionBox_textArea.text="";
			
			var infoObj_object:Object=new Object;
			if (eventObj.detail==Alert.YES)
				infoObj_object.completeRemoval=true;
			else if (eventObj.detail==Alert.NO)
				infoObj_object.completeRemoval=false;
			infoObj_object.message = "deleteModule";
			infoObj_object.selectedModuleIndex = this.module_list.selectedIndex;
			this.sendInfoToController(infoObj_object);
		}
		
		private function openSelectedModule(event:MouseEvent):void
		{
			if(this.module_list.selectedIndex>=0)
			{
				var infoObj_object:Object = new Object;
				infoObj_object.message = "openModule";
				infoObj_object.selectedModuleIndex = this.module_list.selectedIndex;
				infoObj_object.homePageObject = this;
				this.sendInfoToController(infoObj_object);
			}
			else
			{
				Alert.show("Select a module to open!!!!!!!!");
			}
			
		}
		
		private function importNewModule(event:MouseEvent):void
		{
			var file:File = new File;
			var filter:FileFilter = new FileFilter("SplineModule","*.spm");
			try
			{
				file.browseForOpen("Import Module",[filter]);
				file.addEventListener(Event.SELECT, this.moduleSelectedForImport); 
			}
			catch(error:Error)
			{
				trace("HomePageView ----> error :"+error);
			}
		}
		
		private function moduleSelectedForImport(event:Event):void
		{
			var stream:FileStream = new FileStream();
    		var file:File = new File(event.target.nativePath)
    		stream.open(file, FileMode.READ);
    		var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
    		trace("file to be imported: "+fileData+"and path: "+event.target.nativePath+"/"+fileData);
		}
		//------------------------------------------------------------------------------------------------------------------------------------------------//
		
		//-----------------------------------------------------------------Lesson Related Operations ------------------------------------------------------//
		private function addNewLesson(event:MouseEvent):void
		{
			this.nativeWindow_object.enabled = false;
			//var addEditContentWindow:AddEditContentWindow = new AddEditContentWindow("Lesson","AddLesson",this.lessonName_array.length,this);
			var addEditContentWindow:AddEditContentWindow = new AddEditContentWindow("Lesson","AddLesson",this.module_list.selectedIndex,this.lessonName_array.length,this);
			//sending the length as a parameter as this is an optional one u can send any other no. too :)			
		}
		
		private function editSelectedLesson(event:MouseEvent):void
		{
			this.nativeWindow_object.enabled = false;
			var addEditContentWindow:AddEditContentWindow = new AddEditContentWindow("Lesson","EditLesson",this.module_list.selectedIndex,this.lesson_list.selectedIndex,this);
			
		}
		
		private function removeSelectedLesson(event:MouseEvent):void
		{
			if(this.lesson_list.selectedIndex>=0)
			{
				Alert.show("Do you want to remove it from filesystem ?","Confirmation",Alert.YES|Alert.NO,null,this.completeLessonRemovalConfirmation,null,Alert.YES);
			}
			else
			{
				Alert.show("Select a lesson to remove !!!!!");
			}
			
		}
		
		private function completeLessonRemovalConfirmation(eventObj:CloseEvent):void
		{
			this.discriptionBox_textArea.text="";
			
			var infoObj_object:Object=new Object;
			if (eventObj.detail==Alert.YES)
				infoObj_object.completeRemoval=true;
			else if (eventObj.detail==Alert.NO)
				infoObj_object.completeRemoval=false;
			infoObj_object.message = "deleteLesson";
			infoObj_object.selectedModuleIndex=this.module_list.selectedIndex;
			infoObj_object.selectedLessonIndex = this.lesson_list.selectedIndex;
			this.sendInfoToController(infoObj_object);
		}
		
		
		private function openSelectedLessonInTab(event:MouseEvent):void
		{
			if(this.lesson_list.selectedIndex>=0)
			{
				var infoObj_object:Object = new Object;
				infoObj_object.message = "openLesson";
				infoObj_object.selectedModuleIndex = this.module_list.selectedIndex;
				infoObj_object.selectedLessonName = this.lessonName_array[this.lesson_list.selectedIndex];
				//trace("##########$$$$$$$$$"+event+"$$$$$$$$$$$##########");
				infoObj_object.openInNewWindow = false;
				this.sendInfoToController(infoObj_object); 
			}
			else
			{
				Alert.show("Select a lesson to open !!!!!!!!!!");
			}
		}
		
		private function openSelectedLessonInWindow(event:MouseEvent):void
		{
			if(this.lesson_list.selectedIndex>=0)
			{
				var infoObj_object:Object = new Object;
				infoObj_object.message = "openLesson";
				infoObj_object.selectedModuleIndex = this.module_list.selectedIndex;
				infoObj_object.selectedLessonName = this.lessonName_array[this.lesson_list.selectedIndex];
				//trace("##########$$$$$$$$$"+event+"$$$$$$$$$$$##########");
				infoObj_object.openInNewWindow = true;
				this.sendInfoToController(infoObj_object); 
			}
			else
			{
				Alert.show("Select a lesson to open !!!!!!!!!!");
			}
		}
		
		private function importNewLesson(event:MouseEvent):void
		{
			
		}
		//------------------------------------------------------------------------------------------------------------------------------------------------//
		
		
		private function addEditContentWindowClosed():void
		{
			this.nativeWindow_object.enabled = true;
		}
		
		/*private function changeLessonDataModel(infoObj_object:Object):void
		{
			this.sendInfoToController(infoObj_object);
			this.addEditLessonPanel.closeWindow();
			this.addEditLessonPanel.activateNativeWindow();
		}
		
		private function changeCancelled(event:MouseEvent):void
		{
			this.addEditLessonPanel.closeWindow();
			this.addEditLessonPanel.activateNativeWindow();
		}*/
		
		private function updateModuleList(selectedModuleIndex:int):void
		{
			trace("Info recieved from model");
			this.module_list.dataProvider=this.moduleNames_array;
			this.module_list.validateNow();
			this.module_list.selectedIndex = selectedModuleIndex;
			this.showDiscription(this.moduleDiscription_array[this.module_list.selectedIndex]);
			trace("this.moduleNames_array = "+this.moduleNames_array);
			//this.module_list.validateDisplayList();
			
		}
		private function updateLessonList(selectedLessonIndex:int):void
		{
			trace("Lessson info recieved from model");
			this.lesson_list.dataProvider= this.lessonName_array;
			this.lesson_list.validateNow();
			this.lesson_list.selectedIndex = selectedLessonIndex;
			this.showDiscription(this.lessonDiscription_array[this.module_list.selectedIndex]);
			trace("this.lessonName_array = "+this.lessonName_array);
		}
		
		private function showDiscription(discription:String):void
		{
			//will add later
			if(discription != null)
			this.discriptionBox_textArea.text = discription.toString();
		}
		
		
		private function sendInfoToController(infoObj_object:Object):void
		{
			this.homePageController.incomingMessageGateway(infoObj_object);		
		}
		
		
		
		override public function update(model:Observable, infoObj_object:Object):void
		{
			trace ("updating homePageView");
			super.update(model,infoObj_object);
			var message_string:String= infoObj_object.message.toString();
			trace("message to update fuction of homeView : "+message_string);
			switch(message_string)
			{
				case "refreshModuleListDataModel":
					trace("Info recieved from model");
					this.moduleNames_array = infoObj_object.moduleNames_array;
					this.moduleDiscription_array = infoObj_object.moduleDiscription_array;
					this.modulePaths_array = infoObj_object.modulePaths_array;
					this.updateModuleList(infoObj_object.selectedModuleIndex);
					break;
					
				case "refreshLessonListDataModel":
					this.lessonName_array = infoObj_object.lessonName_array;
					this.lessonDiscription_array = infoObj_object.lessonDiscription_array;
					this.lessonPath_array = infoObj_object.lessonPath_array;
					this.lessonEditPass_array = infoObj_object.lessonEditPass_array;
					this.lessonInstructorPass_array = infoObj_object.lessonInstructorPass_array;
					this.updateLessonList(infoObj_object.selectedLessonIndex);
					break;
					
				case "Alert":
					Alert.show(infoObj_object.info.toString(), infoObj_object.type.toString());
					break;
			
			}
		}
		
		public function incomingMessageGateway(infoObj_object:Object):void
		{
			trace("infoObj_object.message = "+infoObj_object.message.toString());
			var message:String=infoObj_object.message.toString();
			
			switch(message)
			{
				case "createNewLesson":
					this.addEditContentWindowClosed();
					this.sendInfoToController(infoObj_object);
					break;
				case "createNewModule":
					this.addEditContentWindowClosed();
					this.sendInfoToController(infoObj_object);
					break;
				case "editLessonMetadata":
					this.addEditContentWindowClosed();
					this.sendInfoToController(infoObj_object);
					break;
				case "editModuleMetadata":
					this.addEditContentWindowClosed();
					this.sendInfoToController(infoObj_object);
					break;
				case "changeCancelled":
					this.addEditContentWindowClosed();
					break;
			}
		}
	}
}